import { getListApi } from "@/api/configCenter/material";
import { reactive, ref } from "vue";
import { getProvinceApi } from "@/api/common";
export default function InitHook() {
  const formState = reactive<Partial<WarehouseEntryChannel.CustomerLevel>>({
    name: undefined,
    type: undefined,
    typeValue: undefined,
    baseProductSize: undefined,
    exceedSize: undefined,
    price: undefined,
    unit: undefined,
    conditionDesc: undefined
  });
  const formDataRef = ref<any>(null);
  const materialOpts = ref<Array<Base.GeneraDataType>>([]);
  const provincesOpts = ref<Array<Base.GeneraDataType>>([]);
  const getAllMaterialOpts = () => {
    return getListApi({ pageNum: 1, pageSize: 99999, status: 1 } as MaterialIndex.SearchForm).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        materialOpts.value = data.records || [];
      }
    });
  };
  const getAllProvincesOpts = () => {
    return getProvinceApi({ level: 2 }).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        provincesOpts.value = data || [];
      }
    });
  };
  const filter = (inputValue: string, path: Array<Base.GeneraDataType>) => {
    return path.some(option => option.name.toLowerCase().indexOf(inputValue.toLowerCase()) > -1);
  };
  return {
    formState,
    formDataRef,
    materialOpts,
    getAllMaterialOpts,
    filter,
    provincesOpts,
    getAllProvincesOpts
  };
}
